using System;
using System.Collections;
using System.Collections.Generic;
using BCW;
using Holoville.HOTween;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MarketControllerUI : MonoBehaviour
{
	public static MarketControllerUI instance;

	public GameObject marketHolder;

	public GameObject bankHolder;

	public UIPanel marketPanel;

	public MarketCategoriesPanel categoriesPanel;

	public MarketSubcategoriesPanel subcategoriesPanel;

	public MarketProductsPanel productsPanel;

	public MarketPreviewController previewController;

	public MarketMainButtons mainButtons;

	public BuyMessagePanel buyingMessage;

	[HideInInspector]
	public CoinShopManager bankManager;

	public DailyCoinsInterface dailyCoins;

	[SerializeField]
	private GameObject freeMoneyHolder;

	private string prefabBank = string.Empty;

	private ResourceRequest bankResourceRequest;

	private bool loadedBank;

	private List<Camera> cameras = new List<Camera>();

	public Light marketLight;

	public Camera mainCamera;

	public bool iPad;

	public int columnsCount;

	private float currentWidth;

	public UITexture backgroundTexture;

	private string currentBackgroundTextreName = string.Empty;

	[SerializeField]
	private GameObject dieMessage;

	public string selectedCategory
	{
		get
		{
			return categoriesPanel.selected;
		}
	}

	public string selectedSubcategory
	{
		get
		{
			return subcategoriesPanel.selected;
		}
	}

	public Product selectedProduct
	{
		get
		{
			return productsPanel.selectedProduct;
		}
	}

	private void Awake()
	{
		instance = this;
		HOTween.Init();
		dailyCoins.Init();
	}

	private void OnDestroy()
	{
		dailyCoins.Unsubscribe();
	}

	public bool MarketActive()
	{
		if (bankHolder != null)
		{
			return bankHolder.activeSelf || marketHolder.activeSelf;
		}
		return marketHolder.activeSelf;
	}

	public IEnumerator ShowBank(MoneyType moneyType = MoneyType.Gold)
	{
		if (bankHolder == null)
		{
			if (loadedBank)
			{
				yield break;
			}
			loadedBank = true;
			LoadingPanel.instance.ShowLoadingPanel(true);
			yield return SceneManager.LoadSceneAsync("Lods_Level_2", LoadSceneMode.Additive);
			bankHolder.transform.localScale = Vector3.one;
			bankManager = bankHolder.GetComponentInChildren<CoinShopManager>();
			bankManager.tabFree = freeMoneyHolder;
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		bankHolder.SetActive(true);
		productsPanel.productInfoPanel.gameObject.SetActive(false);
		switch (moneyType)
		{
		case MoneyType.Cash:
			bankManager.OpenTabCash();
			break;
		case MoneyType.Gold:
			bankManager.OpenTabGold();
			break;
		default:
			bankManager.OpenTabFree();
			break;
		}
	}

	public void ShowFreeHolder()
	{
		dailyCoins.gameObject.SetActive(true);
	}

	private void ToggleCameras(bool active)
	{
		if (mainCamera == null)
		{
			mainCamera = Camera.main;
			cameras.AddRange(mainCamera.GetComponentsInChildren<Camera>());
		}
		cameras.ForEach(delegate(Camera cam)
		{
			cam.enabled = active;
		});
	}

	public void HideBank()
	{
		if (bankHolder != null)
		{
			bankHolder.SetActive(false);
		}
		productsPanel.productInfoPanel.gameObject.SetActive(true);
		if (PromoactionsManagerUI.instance != null && PromoactionsManagerUI.instance.gameObject.activeInHierarchy)
		{
			PromoactionsManagerUI.instance.CreatePromo();
		}
	}

	[Obsolete("Ф-я вызывается только из MarketController")]
	public void ShowMarket(Product product = null)
	{
		if (product == null || !product.categoryName.Equals("Hidden"))
		{
			ToggleCameras(false);
			if (LobbyPlayerGunManager.instance != null)
			{
				LobbyPlayerGunManager.instance.lightObj.SetActive(false);
			}
			if (SceneManager.GetActiveScene().name.Contains("Lobby"))
			{
				marketLight.intensity = 1f;
			}
			else
			{
				marketLight.intensity = 0.7f;
			}
			HideBank();
			GameUI.instance.gameObject.SetActive(false);
			marketHolder.SetActive(true);
			if (product != null)
			{
				ShowProduct(product);
				return;
			}
			categoriesPanel.Select("Weapons");
			categoriesPanel.InitButtons();
		}
	}

	[Obsolete("Ф-я вызывается только из MarketController")]
	public void HideMarket()
	{
		categoriesPanel.Unselect();
		subcategoriesPanel.Unselect();
		productsPanel.Unselect();
		ShowDiedMessege(false);
		GpgWrapper.instance.SaveProducts();
		if (LobbyPlayerGunManager.instance != null)
		{
			LobbyPlayerGunManager.instance.lightObj.SetActive(true);
		}
		if (buyingMessage != null)
		{
			buyingMessage.gameObject.SetActive(false);
		}
		marketHolder.SetActive(false);
		if (bankHolder != null)
		{
			bankHolder.SetActive(false);
		}
		GameUI.instance.gameObject.SetActive(true);
		ToggleCameras(true);
	}

	public void HideMarketUI()
	{
		MarketController.Instance.HideMarket();
	}

	private void ShowProduct(Product product)
	{
		categoriesPanel.Select(product.categoryName, false);
		categoriesPanel.InitButtons();
		subcategoriesPanel.Select(product.subCategoryName, false);
		subcategoriesPanel.InitButtons();
		productsPanel.Select(product.id, false);
		productsPanel.InitButtons();
	}

	public void SetMarketWidth(bool forCars)
	{
		int num = 3;
		if ((float)Screen.width / (float)Screen.height > 1.6f)
		{
			iPad = false;
			num = 4;
			if (forCars)
			{
				num--;
			}
		}
		else
		{
			iPad = true;
			num = 3;
			if (forCars)
			{
				num--;
			}
		}
		float num2 = (float)num * productsPanel.buttonsGrid.cellWidth + categoriesPanel.panel.width + 15f;
		if (currentWidth != num2)
		{
			columnsCount = num;
			currentWidth = num2;
			productsPanel.buttonsGrid.maxPerLine = num;
			marketPanel.rightAnchor.Set(0f, num2);
			marketPanel.UpdateAnchors();
			categoriesPanel.panel.UpdateAnchors();
			subcategoriesPanel.panel.UpdateAnchors();
			productsPanel.panel.UpdateAnchors();
			previewController.widget.UpdateAnchors();
			mainButtons.widget.UpdateAnchors();
			marketPanel.gameObject.SetActive(false);
			marketPanel.gameObject.SetActive(true);
		}
	}

	public void SetBackgroundTexture(string textreName)
	{
		if (!currentBackgroundTextreName.Equals(textreName))
		{
			backgroundTexture.mainTexture = Resources.Load("TextureForPreload/" + textreName) as Texture;
		}
	}

	public void ShowDiedMessege(bool val)
	{
		if (MarketActive())
		{
			dieMessage.SetActive(val);
		}
	}
}
